package zjut.raymond.collectorplus.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import zjut.raymond.collectorplus.R;

public class CustomTitleView extends FrameLayout{
    private TextView titleView;
    private ImageView leftImg;
    private ImageView rightImg;
    private String title;
    private Drawable leftDrawable;
    private Drawable rightDrawable;

    public CustomTitleView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public CustomTitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initTypedArray(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.custom_title_view, this);
        titleView = findViewById(R.id.tv_title);
        leftImg = findViewById(R.id.img_left);
        rightImg = findViewById(R.id.img_right);
        titleView.setText(title);
        leftImg.setImageDrawable(leftDrawable);
        rightImg.setImageDrawable(rightDrawable);
    }

    private void initTypedArray(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView);
        title = typedArray.getString(R.styleable.CustomTitleView_title_text);
        leftDrawable = typedArray.getDrawable(R.styleable.CustomTitleView_left_img_src);
        rightDrawable = typedArray.getDrawable(R.styleable.CustomTitleView_right_img_src);
        typedArray.recycle();
    }

    public void setLeftOnClickListener(OnClickListener onClickListener) {
        leftImg.setOnClickListener(onClickListener);
    }

    public void setRightOnClickListener(OnClickListener onClickListener) {
        rightImg.setOnClickListener(onClickListener);
    }

    public void setTitle(String titleName){
        if(!TextUtils.isEmpty(titleName)) {
            titleView.setText(titleName);
        }
    }

}
